package com.bit.strongbaby;

import android.hardware.*;
import android.app.Activity;
import android.content.*;
import android.graphics.*;
import android.graphics.Paint.*;
import android.media.MediaPlayer;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Environment;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.OrientationEventListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;

public class GyroActivity extends Activity {

	SensorManager mSensorManager;
	SensorEventListener gyroListener;
	Sensor gyroSensor;
	SensorEvent event;
	SoundPool pool;
	int mOrientation = -1;
	int ddok;
	float x;
	private Vibrator tVibrator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_gyro);
		OrientationEventListener mOrientationListener = null;
		ImageView img = (ImageView) findViewById(R.id.ddalang);
		TranslateAnimation ani = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, -0.1f, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, -0.1f);
		ani.setDuration(1000);
		ani.setRepeatCount(Animation.INFINITE);
		ani.setRepeatMode(Animation.RESTART);

		img.startAnimation(ani);
		mOrientationListener = new OrientationEventListener(this,
				SensorManager.SENSOR_DELAY_FASTEST) {
			public void onOrientationChanged(int orientation) {
				mOrientation = orientation;
				// mView.invalidate();
			}
		};
		mOrientationListener.enable();

		// setContentView(R.layout.activity_gyro);
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		gyroSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
		tVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

		// 객체를 생성
		pool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		// ddok에 사운드를 로드함
		ddok = pool.load(this, R.raw.toy, 1);
		// xml에 생성한 값들을 불러옴

		gyroListener = new SensorEventListener() {

			@Override
			public void onAccuracyChanged(Sensor sensor, int accuracy) {
			}

			@Override
			public void onSensorChanged(SensorEvent event) {
				float[] values = event.values;
				float x = values[0];
				float y = values[1];
				float z = values[2];
				long[] vibratePattern = { 100, 100, 300 };

				TextView x_Value = (TextView) findViewById(R.id.text_x);
				TextView y_Value = (TextView) findViewById(R.id.text_y);
				TextView z_Value = (TextView) findViewById(R.id.text_z);

				if (x > 1.5 && z < -1.0 && y < -0.7) {
					pool.play(ddok, 10, 10, 0, 0, 1);
					tVibrator.vibrate(300);
					tVibrator.vibrate(vibratePattern, -1);
				}

				// (soundID, leftVolume, rightVolume, priority, loop, rate)
				if (x < -1.5 && z > 1.0 && y > 0.7) {
					pool.play(ddok, 10, 10, 0, 0, 1);
					tVibrator.vibrate(300);
					tVibrator.vibrate(vibratePattern, -1);
				}
			}
		};
	}

	@Override
	public void onPause() {
		super.onPause();
		mSensorManager.unregisterListener(gyroListener);
	}

	@Override
	public void onResume() {
		super.onResume();
		mSensorManager.registerListener(gyroListener, gyroSensor,
				SensorManager.SENSOR_DELAY_GAME);
	}
}